package thread;

import java.util.LinkedList;

/**
 * 消费者，生产者模型
 */
public class PrdCostuom {

    private static LinkedList<Integer> list = new LinkedList<>();

    private volatile static int canGet = 0;

    private volatile static int end = 0;

    public static void main(String[] args) {
        Thread productor = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    list.add(i);
                    System.out.println("往队列中加入" + i);
                    canGet = 1;
                }
                end = 1;
            }
        });

        Thread customer = new Thread(new Runnable() {
            @Override
            public void run() {
                while (end != 1 || list.peek() != null){
                    if(canGet == 0){
                        continue;
                    }
                    Integer pop = list.pop();
                    System.out.println("取出首个元素" + pop);
                }
            }
        });

        productor.start();
        customer.start();


    }



}
